package com.skyolder.milkbee.web.rest.my;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.codahale.metrics.annotation.Timed;
import com.skyolder.milkbee.service.SweepstakeService;
import com.skyolder.milkbee.service.dto.SweepstakeDTO;
import com.skyolder.milkbee.web.rest.util.HeaderUtil;
import com.skyolder.milkbee.web.rest.util.PaginationUtil;

import io.swagger.annotations.ApiParam;

/**
 * REST controller for managing Sweepstake.
 */
@RestController("mySweepstakeResource")
@RequestMapping("/api/my")
public class SweepstakeResource {

    private final Logger log = LoggerFactory.getLogger(SweepstakeResource.class);
        
    @Inject
    private SweepstakeService sweepstakeService;

    /**
     * POST  /sweepstakes : Create a new sweepstake.
     *
     * @param sweepstakeDTO the sweepstakeDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new sweepstakeDTO, or with status 400 (Bad Request) if the sweepstake has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/sweepstakes")
    @Timed
    public ResponseEntity<SweepstakeDTO> createSweepstake(@RequestBody SweepstakeDTO sweepstakeDTO) throws URISyntaxException {
        log.debug("REST request to save Sweepstake : {}", sweepstakeDTO);
        if (sweepstakeDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("sweepstake", "idexists", "A new sweepstake cannot already have an ID")).body(null);
        }
        SweepstakeDTO result = sweepstakeService.save(sweepstakeDTO);
        return ResponseEntity.created(new URI("/api/sweepstakes/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("sweepstake", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /sweepstakes : Updates an existing sweepstake.
     *
     * @param sweepstakeDTO the sweepstakeDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated sweepstakeDTO,
     * or with status 400 (Bad Request) if the sweepstakeDTO is not valid,
     * or with status 500 (Internal Server Error) if the sweepstakeDTO couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/sweepstakes")
    @Timed
    public ResponseEntity<SweepstakeDTO> updateSweepstake(@RequestBody SweepstakeDTO sweepstakeDTO) throws URISyntaxException {
        log.debug("REST request to update Sweepstake : {}", sweepstakeDTO);
        if (sweepstakeDTO.getId() == null) {
            return createSweepstake(sweepstakeDTO);
        }
        SweepstakeDTO result = sweepstakeService.save(sweepstakeDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("sweepstake", sweepstakeDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /sweepstakes : get all the sweepstakes.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of sweepstakes in body
     * @throws URISyntaxException if there is an error to generate the pagination HTTP headers
     */
    @GetMapping("/sweepstakes")
    @Timed
    public ResponseEntity<List<SweepstakeDTO>> getAllSweepstakes(@ApiParam Pageable pageable)
        throws URISyntaxException {
        log.debug("REST request to get a page of Sweepstakes");
        Page<SweepstakeDTO> page = sweepstakeService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/my/sweepstakes");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /sweepstakes/:id : get the "id" sweepstake.
     *
     * @param id the id of the sweepstakeDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the sweepstakeDTO, or with status 404 (Not Found)
     */
    @GetMapping("/sweepstakes/{id}")
    @Timed
    public ResponseEntity<SweepstakeDTO> getSweepstake(@PathVariable Long id) {
        log.debug("REST request to get Sweepstake : {}", id);
        SweepstakeDTO sweepstakeDTO = sweepstakeService.findOne(id);
        return Optional.ofNullable(sweepstakeDTO)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /sweepstakes/:id : delete the "id" sweepstake.
     *
     * @param id the id of the sweepstakeDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/sweepstakes/{id}")
    @Timed
    public ResponseEntity<Void> deleteSweepstake(@PathVariable Long id) {
        log.debug("REST request to delete Sweepstake : {}", id);
        sweepstakeService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("sweepstake", id.toString())).build();
    }

}
